﻿using System;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using DataLite.Values;

namespace DataLite
{
    public class Tools
    {
        const string ITEM = "item";
        const string KEY = "key";
        const string VALUE = "value";

        public static Hashtable LoadConfig(string aPathFile, string aNode)
        {
            XmlDocument oXmlDocument = new XmlDocument();
            oXmlDocument.Load(aPathFile);

            XmlNodeList oSelectedNode = oXmlDocument.GetElementsByTagName(aNode);
            XmlNodeList oItems = oXmlDocument.GetElementsByTagName(ITEM);

            Hashtable oHashtable = new Hashtable();
            foreach (XmlElement oNodo in oItems)
            {
                int i = 0;
                XmlNodeList oKey = oNodo.GetElementsByTagName(KEY);
                XmlNodeList oValue = oNodo.GetElementsByTagName(VALUE);
                oHashtable.Add(oKey[i].InnerText, oValue[i].InnerText);
            }

            return oHashtable;
        }

        /// <summary>
        /// Formate el nombre de las tablas y columnas 
        /// </summary>
        /// <param name="aDbName">cadena sin formato</param>
        /// <returns>cadena formateada</returns>
        public static string FormatDbName(string aDbName)
        {
            StringBuilder oStringBuilder = new StringBuilder();
            char oCurrent;
            int oState = 0;

            for (int i = 0; i < aDbName.Length; i++)
            {
                oCurrent = aDbName[i];
                if (i == 0) { oStringBuilder.Append(char.ToUpper(oCurrent)); }
                else
                {
                    if (oCurrent == ' ' || oCurrent == '_') { oState = 1; }
                    else
                    {
                        if (oState == 0) { oStringBuilder.Append(char.ToLower(oCurrent)); }
                        else
                        {
                            oStringBuilder.Append(char.ToUpper(oCurrent));
                            oState = 0;
                        }
                    }
                }
            }
            return oStringBuilder.ToString();
        }

        public static string FormatColumns(SqliteMasterVO rSqliteMasterVO)
        {
            string oFields = string.Empty;
            for (int indx = 0; indx < rSqliteMasterVO.ColumnInfo.Count; indx++)
            {
                oFields += rSqliteMasterVO.ColumnInfo[indx].Name;
                if (indx < rSqliteMasterVO.ColumnInfo.Count - 1)
                {
                    oFields += ", ";
                }
            }
            return oFields;
        }

        public static string FormatParameters(SqliteMasterVO rSqliteMasterVO)
        {
            string oParameters = string.Empty;
            for (int indx = 0; indx < rSqliteMasterVO.ColumnInfo.Count; indx++)
            {
                oParameters += "@" + rSqliteMasterVO.ColumnInfo[indx].Name;
                if (indx < rSqliteMasterVO.ColumnInfo.Count - 1)
                {
                    oParameters += ", ";
                }
            }
            return oParameters;
        }

        public static string ConditionPrimaryKey(SqliteMasterVO rSqliteMasterVO)
        {
            List<ColumnInfoVO> oPrimaryKey = new List<ColumnInfoVO>();
            for (int indx = 0; indx < rSqliteMasterVO.ColumnInfo.Count; indx++)
            {
                if (rSqliteMasterVO.ColumnInfo[indx].Pk == 1)
                {
                    oPrimaryKey.Add(rSqliteMasterVO.ColumnInfo[indx]);
                }
            }

            string oCondition = string.Empty;
            for (int indx = 0; indx < oPrimaryKey.Count; indx++)
            {
                oCondition += string.Format("{0} = @{0}", oPrimaryKey[indx].Name);
                if (indx < oPrimaryKey.Count - 1)
                {
                    oCondition += " AND ";
                }
            }
            return oCondition;
        }

        public static string ConditionUpdateAll(SqliteMasterVO sqliteMasterVO)
        {
            StringBuilder oStringBuilder = new StringBuilder();

            for (int indx = 0; indx < sqliteMasterVO.ColumnInfo.Count; indx++)
            {
                ColumnInfoVO columnInfoVO = sqliteMasterVO.ColumnInfo[indx];
                if (sqliteMasterVO.ColumnInfo[indx].Pk != 1)
                {
                    oStringBuilder.AppendFormat("{0} = @{0} ", columnInfoVO.Name);
                    if (indx < sqliteMasterVO.ColumnInfo.Count - 1)
                    {
                        oStringBuilder.Append(", ");
                    }
                }
            }
            return oStringBuilder.ToString();
        }

        public static string FormarList(List<string> rList)
        {
            StringBuilder oStringBuilder = new StringBuilder();
            for (int indx = 0; indx < rList.Count; indx++)
            {
                oStringBuilder.Append(rList[indx]);
                if (indx < rList.Count - 1)
                {
                    oStringBuilder.AppendLine(", ");
                }
            }
            return oStringBuilder.ToString();
        }

        public static string FormarParagraph(List<string> rList)
        {
            StringBuilder oStringBuilder = new StringBuilder();
            for (int indx = 0; indx < rList.Count; indx++)
            {
                oStringBuilder.AppendLine(rList[indx]);
                if (indx < rList.Count - 1)
                {
                    oStringBuilder.AppendLine();
                }
            }
            return oStringBuilder.ToString();
        }
    }
}
